package com.barbarosso.utils.loader.types {
	import com.barbarosso.utils.loader.AssetEvent;	
	
	import flash.display.Bitmap;	
	
	import nl.demonsters.debugger.MonsterDebugger;	
	
	import flash.net.URLRequest;	
	import flash.errors.IllegalOperationError;	
	import flash.events.IOErrorEvent;	
	
	import com.barbarosso.utils.loader.types.AbstractAsset;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.ProgressEvent;		

	/**
	 * @author barbarosso
	 */
	public class ImageAsset extends AbstractAsset {

		private static const JPG 	: String = "jpg";
		private static const GIF 	: String = "gif";

		private var _loader 		: Loader;
		private var _urlRequest 	: URLRequest;
		
		public function ImageAsset(p_id : String, p_source : String, p_fileSize : int = 0) {
			
			if(isValidType(p_source) && p_id.length > 4) {
				super(p_id  , p_source , p_fileSize);
			}else{
				throw IllegalOperationError("not a valid file extention in your source");
			}
			
		}
		/*
		 * protected methods ----------------------------------------------
		 */
		override public function load() : void {
			if(!isLoaded){
				_urlRequest = new URLRequest(_source);
				_loader = new Loader();
				_loader.load(_urlRequest);
				addEventListeners();
			}
		}
		
		override public function cancel() : void {
			MonsterDebugger.trace(this,isLoaded +" = loading condition -- cancel = " + this.id);
			if(!isLoaded){
				MonsterDebugger.trace(this, "cancel is not loaded = " + this.id);
				removeEventListeners();
				try{
		 			_loader.close();
					MonsterDebugger.trace(this, "canceled = " + this.id);
				}catch(e:Error){
					throw IllegalOperationError("cancel did not succeed! are you in the browser?");
				}
			}
		}
		
		override public function getAsset() : Object {
			if(isLoaded){
				return _loader.content as Bitmap;
			}
			return null;
		}
		
		
		/*
		 * protected methods ----------------------------------------------
		 */
		override protected function isValidType( source : String ) : Boolean {
				switch (extractFileType(source))
				{
					case JPG:
					case GIF:
						return true;
						break;
					default:
						return false;
						break;
				}
		}
		
		/*
		* complete:Event — Dispatched by the associated LoaderInfo object when the file has completed loading. The complete event is always dispatched after the init event.
    	* httpStatus:HTTPStatusEvent — Dispatched by the associated LoaderInfo object when a network request is made over HTTP and Flash Player can detect the HTTP status code.
   	 	* init:Event — Dispatched by the associated LoaderInfo object when the properties and methods of the loaded SWF file are accessible. The init event always precedes the complete event.
    	* ioError:IOErrorEvent — Dispatched by the associated LoaderInfo object when an input or output error occurs that causes a load operation to fail.
    	* open:Event — Dispatched by the associated LoaderInfo object when the loading operation starts.
    	* progress:ProgressEvent — Dispatched by the associated LoaderInfo object as data is received while load operation progresses.
    	* unload:Event — Dispatched by the associated LoaderInfo object when a loaded object is removed.

		 */
		protected function addEventListeners() : void {
			 	_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
			 	_loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
			 	_loader.contentLoaderInfo.addEventListener(Event.OPEN, openHandler);
				_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
				_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
		}
		 
		 protected function removeEventListeners() : void {
			 	_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
			 	_loader.contentLoaderInfo.removeEventListener(Event.INIT, initHandler);
			 	_loader.contentLoaderInfo.removeEventListener(Event.OPEN, openHandler);
				_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
				_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
		 }
		 
		/*
		 * event handler methods ----------------------------------------------
		 */ 		
		private function completeHandler( event : Event) : void {
			 	removeEventListeners();
				_isLoaded = true;
				MonsterDebugger.trace(this, _source + "has done loading " + _loader.contentLoaderInfo.bytesLoaded);
				dispatchEvent(new AssetEvent(AssetEvent.COMPLETE));
		}
		
		private function progressHandler( event : ProgressEvent) : void {
		 		var percentLoaded:Number = event.bytesLoaded/event.bytesTotal;
				percentLoaded = Math.round(percentLoaded * 100);
				//MonsterDebugger.trace(this ,"Loading: "+percentLoaded+"%");
		 }
		 
		private function initHandler( event : Event) : void {
				MonsterDebugger.trace(this, _source + " initHandler  " );
		 }
		 
		private function openHandler( event : Event) : void {
				MonsterDebugger.trace(this, _source + " openHandler  " );
		 }
		 
		private function errorHandler( event : IOErrorEvent) : void {
				MonsterDebugger.trace(this, _source + " IOErrorEvent =  " + event.toString());
		 }
	}
}
